package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Service
//@Transactional mongodb事务必须是复制集方式
public class DestinationServiceImpl implements IDestinationService{


    @Autowired
    private DestinationRepository repository;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private MongoTemplate template;


    @Override
    public void save(Destination destination) {
        destination.setId(null);
        repository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        repository.save(destination);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return repository.findAll();
    }

    @Override
    public List<Destination> queryDesByRegionId(String rid) {
        //通过区域id查询区域对象
        Region region = regionService.get(rid);
        //获取目的地挂载的目的地id集合
        List<String> ids = region.getRefIds();
        return repository.findByIdIn(ids);
    }

    @Override
    public void saveOrUpdate(Destination destination) {
        if (StringUtils.hasLength(destination.getId())){
            this.update(destination);
        }else {
            this.save(destination);
        }
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {
        Query query = new Query();
        //条件
        if (StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }
        if (StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else {
            //为null
            query.addCriteria(Criteria.where("parentId").is(null));
        }


        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.DESC,"_id");
        return DBHelper.query(template,Destination.class,query,pageable);
    }

    @Override
    public List<Destination> queryToasts(String parentId) {
        List<Destination> list = new ArrayList<>();
        //吐司查询
        //循环
        //递归
        this.createToast(list,parentId);
        Collections.reverse(list);//list集合翻转
        return list;
    }



    //用于创建toast
    private void createToast(List<Destination> list, String parentId) {
        //当parentId为空的情况向 不执行
        if (!StringUtils.hasLength(parentId)){
            return;
        }
        Destination dest = this.get(parentId);
        list.add(dest);
        if (StringUtils.hasLength(dest.getParentId())){
            createToast(list,dest.getParentId());
        }
    }
    @Override
    public List<Destination> queryDesByRegionIdForApi(String regionId) {
        List<Destination> list = new ArrayList<>();
        //第一层
        //国内
        if ("-1".equals(regionId)){
            //中国下面所有的省份
            list = repository.findByParentName("中国");
        }else {
            //非国内
            list = this.queryDesByRegionId(regionId);
        }
        //第二层
        for (Destination dest :list){
            //约定:查询子目的地只需要5个
//            if (children.size()>5){
//                children = children.subList(0,5);
//            }
            //jpa 查询方法 可以添加分页排序操作

            Pageable pageable = PageRequest.of(0,5, Sort.Direction.ASC,"_id");
            List<Destination> children = repository.findByParentId(dest.getId(),pageable);
            dest.setChildren(children);
        }

        return list;
    }

    @Override
    public Destination queryName(String name) {
        return repository.findByName(name);
    }


}
